package com.yuanfeng.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.TokenUtils;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.order.entity.OrderInvoiceTemplateInfoEntity;
import com.yuanfeng.order.mapper.OrderInvoiceTemplateInfoMapper;
import com.yuanfeng.order.service.OrderInvoiceTemplateInfoService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @ClassName OrderInvoiceTemplateInfoServiceImpl
 * @Description  发票模版管理表
 * @Author cyp
 * @Date 2023-07-04 13:32:35
 * @Version 1.0
 **/
@Service
public class OrderInvoiceTemplateInfoServiceImpl extends ServiceImpl<OrderInvoiceTemplateInfoMapper, OrderInvoiceTemplateInfoEntity>
        implements OrderInvoiceTemplateInfoService {

    @Override
    public ResponseResult queryInvoiceInfoList(Map<String,Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == userInfo){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        try {
            param.put("userId",userInfo.getUserId());
            //分页查询数据
            IPage<OrderInvoiceTemplateInfoEntity> page = this.baseMapper.queryInvoiceInfoList(new Query<OrderInvoiceTemplateInfoEntity>().getPage(param),param);

            return ResponseResult.success(PageUtils.getPage(page));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR.getCode(),BizCodeEnume.SYSTEM_ERROR.getMsg());
    }

    @Override
    public ResponseResult insertInvoiceTmp(Map<String, Object> param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.get("token").toString());
        if(null == userInfo){
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }
        try {
            param.put("userId",userInfo.getUserId());
            if (param.get("invoiceTmpId") == null) {
                Integer count = this.baseMapper.queryInvoiceInfoListCount(param);
                //发票模版大于10个无法添加
                if(null !=count && count >= 10){
                    return ResponseResult.fail(BizCodeEnume.INVOICE_TEMPLATES_HAS_SUPERIOR_LIMIT);
                }
                int isDefault = (Integer) param.get("isDefault");
                //如果新插入的为默认配置，则将其他的配置isDefault置为0
                if (1 == isDefault) {
                    this.baseMapper.updateDefault(param);
                }
                this.baseMapper.insertInvoiceTmp(param);
            } else {
                this.baseMapper.updateInvoiceTmp(param);
            }
            return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseResult.fail(BizCodeEnume.SYSTEM_ERROR);

    }

    @Override
    public ResponseResult deleteInvoiceTmp(Map<String, Object> param) {
            String token = (String) param.get("token");
            ResponseResult tokenResult = TokenUtils.checkToken(token);
            if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
                return tokenResult;
            } else {
                param.put("userId", tokenResult.getData().toString());
            }
            try {
                this.baseMapper.delete(new LambdaQueryWrapper<OrderInvoiceTemplateInfoEntity>()
                        .eq(OrderInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                        .eq(OrderInvoiceTemplateInfoEntity::getInvoiceTmpId, param.get("invoiceTmpId").toString()));
                return ResponseResult.success();
            } catch (Exception e) {
                log.error(e.getMessage());
                return ResponseResult.fail();
            }
    }

    @Override
    public ResponseResult queryInvoiceTmp(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        try {
            OrderInvoiceTemplateInfoEntity invoiceTmpEntity = this.baseMapper.selectById(param.get("invoiceTmpId").toString());
            return ResponseResult.success(invoiceTmpEntity);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }

    }

    @Override
    public ResponseResult updateInvoiceTmp(Map<String, Object> param) {
        String token = (String) param.get("token");
        ResponseResult tokenResult = TokenUtils.checkToken(token);
        if (tokenResult.getCode() == CommonStatusEnum.FAIL.getCode()) {
            return tokenResult;
        } else {
            param.put("userId", tokenResult.getData().toString());
        }
        try {
            if (param.get("invoiceType").equals(1)) {
                param.put("invoiceHeadType", 2);
            }
            int isDefault = Integer.valueOf(param.get("isDefault").toString());
            //如果新插入的为默认配置，则将其他的配置isDefault置为0
            if (1 == isDefault) {
                this.baseMapper.update(null, new LambdaUpdateWrapper<OrderInvoiceTemplateInfoEntity>()
                        .eq(OrderInvoiceTemplateInfoEntity::getUserId, param.get("userId").toString())
                        .set(OrderInvoiceTemplateInfoEntity::getIsDefault, 0));
            }
            this.baseMapper.updateInvoiceTmp(param);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseResult.fail(e.getMessage());
        }

    }

}
